<?php
declare(strict_types=1);
/**
 * 云仓API中心
 */
namespace Service\VenderClient\YunCangApi;
use Service\Code\Normal;
use Service\BaseService;
use Service\ExtraService\TaskWorker;
use Service\HttpServer\Http;
class YunCangApiClient extends BaseService
{
    protected $config;
    public function __construct( array $apiBody = [] )
    {
        $this->config = $apiBody;
    }
    
    /**
     * 推送接口
     * @param string $method
     * @param array $body
     */
    public function callApi(string $method ,array $body)
    {
        $param['appid']         = $this->config['stb_app_id'];
        $param['requestMethod'] = $method;
        $param['requestData']   = rawurlencode(json_encode($body));
        $param['sign']          = strtoupper(Http::getSign($param,$this->config['stb_app_secret']));        
        $parameter = [
            [
                'tag'       => 'yuncang_push_'.$method,
                'url'       => $this->config['stb_url'],
                'data'      => $param,
                'method'    => 'post',
                'format'    => 'x-www-form-urlencoded',
                'header'    => [],
            ],
        ];
        $resTask    = TaskWorker::instance()->addTask($parameter);
        $resTask    = current($resTask);
        if($resTask['response']['result']){
            $response   = @json_decode($resTask['response']['result'],true);
            if( isset($response['code']) ) {
                return $response;
            }
        }
        return Normal::returnCode(Normal::FAIL,"请求失败");
    }
}
